import 'dart:convert';

class ParentGetHomeworksModel {
  String id;
  String content, title, time, clsName, subject;
  String students;
  String student;
  String systemQuestions;
  var systemAnswers;
  String answer;
  String checkStatus;
  String checkDescription;
  String checkScore;
  String teacherHomework;
  String publishTime;
  String teacherName;
  String isRemind;
  String studentId;
  String isRepulse;
  String studentName, clsMapping;
  String status;
  String teacherId;
  List answerImgs;
  List imgs;
  InfosModel infosModel;

  //自己写的一个时间
  String monthDay;

  ParentGetHomeworksModel({this.id});

  ParentGetHomeworksModel.fromJson(Map<String, dynamic> json) {
    id = "${json['id']}";
    content = json['content'];
    title = json['title'];
    imgs = json['imgs'];
    time = json['time'];
    DateTime dateTime = DateTime.parse(time);
    monthDay = "${dateTime.month}月${dateTime.day}日";

    clsName = json['cls_name'];
    subject = json['subject'];
    teacherName = json['teacher_name'];
    //================公共的===============
    //================教师=================
    if (json["students"] != null) {
      List studentsList = jsonDecode(json["students"]);
      if (studentsList.length > 0) {
        studentsList.forEach((element) {
          students = "${students ?? ""}${element["name"]},";
        });

        students = students.substring(0, students.length - 1);
      }
    }

    clsMapping = "${json["cls_mapping"]}";
    teacherId = json["teacher_id"];
    if (json['infos'] != null) {
      infosModel = InfosModel.fromJson(json['infos']);
    }

    status = json["status"];

    //================教师=============
    //================学生============
    student = "${json['student']}";
    //这是个字符串
    systemQuestions = json['system_questions'];
    systemAnswers = json['system_answers'];
    answer = json['answer'];
    //图片数组
    answerImgs = json['answer_imgs'];
    checkStatus = json['check_status'];
    checkDescription = json['check_description'];
    checkScore = json['check_score'];
    //发布时间
    if (json['publish_time'] != null) {
      DateTime dateTimePublishTime = DateTime.parse(json['publish_time']);
      publishTime =
          "发布于:${dateTimePublishTime.month}-${dateTimePublishTime.day}\t${dateTimePublishTime.hour}:${dateTimePublishTime.minute}";
    } else {
      publishTime =
          "发布于:${dateTime.month}-${dateTime.day}\t${dateTime.hour}:${dateTime.minute}";
    }

    teacherHomework = "${json['teacher_homework']}";
    isRemind = json['is_remind'];
    studentName = json['student_name'];
    studentId = json['student_id'];
    isRepulse = json['is_repulse'];
    //================学生============

//    if (json['answer'] != null) {
//      answer = new List<Answer>();
//      json['answer'].forEach((v) {
//        answer.add(new Answer.fromJson(v));
//      });
//    }
  }

  //判断状态
  String switchStatus(bool identity) {
    if (identity) {
//      # 1 : 进行中; 2: 待批改; 3 :已批改
      if (status == "1") {
        return "进行中";
      } else if (status == "2") {
        return "待批改";
      } else if (status == "3") {
        return "已批改";
      }
    } else {
      if (checkStatus == "1") {
        return "待批改";
      } else if (checkStatus == "2") {
        return "已批改";
      } else if (checkStatus == "3") {
        if (isRepulse == "1") {
          return "被打回";
        } else {
          return "未提交";
        }
      }
    }
    return "还没判断";
  }

  String getTitleStatus(identity) {
    if (identity) {
//      # 1 : 进行中; 2: 待批改; 3 :已批改
      if (status == "1") {
        return "进行中";
      } else if (status == "2") {
        return "待批改";
      } else if (status == "3") {
        return "已批改";
      }
    } else {
      if (status == "1") {
        return "作业已提交,等待老师批改~";
      } else if (status == "2") {
        return "";
      } else if (status == "3") {
        if (isRepulse == "1") {
          return "作业需要重新提交~";
        } else {
          return "作业还未提交~";
        }
      }
    }
    return "还没判断";
  }

  bool getTitleBool(identity) {
    if (identity) {
//      # 1 : 进行中; 2: 待批改; 3 :已批改
      if (status == "1") {
        return true;
      } else if (status == "2") {
        return true;
      } else if (status == "3") {
        return true;
      }
    } else {
//      # 1 : 未批改; 2: 已批改; 3 :未提交
      if (status == "1") {
        return false;
      } else if (status == "2") {
        return true;
      } else if (status == "3") {
        if (isRepulse == "1") {
          return true;
        } else {
          return true;
        }
      }
    }
    return false;
  }

  String buttonStatus(bool identity) {
    if (identity) {
      if (status == "2") {
        return "去批改";
      }
    } else {
      if (checkStatus == "1") {
        return "查看";
      } else if (checkStatus == "2") {
        return "";
      } else if (checkStatus == "3") {
        return "去提交";
      }
    }
    return "还没判断";
  }

  bool panduanBool(identity) {
    if (identity) {
      return status == "2";
    } else {
      if (checkStatus == "2") {
        return false;
      } else if (checkStatus == "3" || checkStatus == "1") {
        return true;
      }
    }
    return false;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['content'] = this.content;

//    if (this.answer != null) {
//      data['answer'] = this.answer.map((v) => v.toJson()).toList();
//    }
    return data;
  }
}

class InfosModel {
  String noSubmit;
  String toCheck;
  String check;
  String all;

  InfosModel({this.noSubmit, this.check, this.all, this.toCheck});

  InfosModel.fromJson(Map<String, dynamic> json) {
    noSubmit = "${json['no_submit'] ?? 0}";
    toCheck = "${json['to_check'] ?? 0}";
    check = "${json['check'] ?? 0}";
    all = "${json['all'] ?? 0}";
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['no_submit'] = this.noSubmit;
    data['to_check'] = this.toCheck;
    data['check'] = this.check;
    data['all'] = this.all;
    return data;
  }
}
